from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()

class Student(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique=True, nullable=False)  # 学号
    name = db.Column(db.String(50), nullable=False)
    gender = db.Column(db.String(10))
    grade = db.Column(db.String(20))
    class_name = db.Column(db.String(20))
    department = db.Column(db.String(50))
    phone = db.Column(db.String(20))
    email = db.Column(db.String(100))
    password_hash = db.Column(db.String(256), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    honors = db.relationship(
        'Honor',
        backref='student',
        lazy=True,
        cascade="all, delete-orphan"
    )
    avatar_url = db.Column(db.String(255))  # 头像URL

    def set_password(self, password):
        from werkzeug.security import generate_password_hash
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        from werkzeug.security import check_password_hash
        return check_password_hash(self.password_hash, password)

class Teacher(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique=True, nullable=False)  # 工号
    name = db.Column(db.String(50), nullable=False)
    gender = db.Column(db.String(10))
    department = db.Column(db.String(50))
    phone = db.Column(db.String(20))
    email = db.Column(db.String(100))
    password_hash = db.Column(db.String(256), nullable=False)
    is_approved = db.Column(db.Boolean, default=False)  # 审核状态
    approval_status = db.Column(db.String(20), default='pending')  # pending, approved, rejected
    approval_time = db.Column(db.DateTime)
    approved_by = db.Column(db.Integer, db.ForeignKey('admin.id'))
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    def set_password(self, password):
        from werkzeug.security import generate_password_hash
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        from werkzeug.security import check_password_hash
        return check_password_hash(self.password_hash, password)

class Admin(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique=True, nullable=False)
    name = db.Column(db.String(50), nullable=False)
    password_hash = db.Column(db.String(256), nullable=False)
    email = db.Column(db.String(100))
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    approvals = db.relationship('Teacher', backref='approver', lazy=True)

    def set_password(self, password):
        from werkzeug.security import generate_password_hash
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        from werkzeug.security import check_password_hash
        return check_password_hash(self.password_hash, password)

class Honor(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    student_id = db.Column(
        db.Integer,
        db.ForeignKey('student.id', ondelete='CASCADE'),
        nullable=False
    )
    title = db.Column(db.String(100), nullable=False)
    honor_type = db.Column(db.String(50))
    level = db.Column(db.String(50))
    rank = db.Column(db.String(20))
    organization = db.Column(db.String(100))
    description = db.Column(db.String(255))
    date = db.Column(db.String(20))
    certificate_url = db.Column(db.String(255))
    created_at = db.Column(db.DateTime)